<?php
/**
 * Created by PhpStorm.
 * User: zh
 * Date: 16/5/4
 * Time: 上午9:41
 */
class FinanceStatement extends Eloquent
{
    protected $table = "finance_statement";

    public static function putSXPRecords($start,$stop){
        $data = SuixingpayPushRecord::whereBetween('tranDt',[$start,$stop])->get();
        Log::info($data);
        foreach($data as $sxp) {
            $fs = FinanceStatement::where('sxp_id', $sxp->id)->first();
            if(!$fs){
                $fs = new FinanceStatement();
                $fs->created_at = Common::currentTimeString();
            }
            $fs->sxp_id = $sxp->id;
            $fs->tranSts = $sxp->tranSts;
            $fs->tranTm = $sxp->tranTm;
            $fs->authCode = $sxp->authCode;
            $fs->batNo = $sxp->batNo;
            $fs->sign = $sxp->sign;
            $fs->trmNo = $sxp->trmNo;
            $fs->paySource = $sxp->paySource;
            $fs->cardNo = $sxp->cardNo;
            $fs->feeAmt = $sxp->feeAmt;
            $fs->tranSeqNo = $sxp->tranSeqNo;
            $fs->mno = $sxp->mno;
            $fs->tranType = $sxp->tranType;
            $fs->ordNo = $sxp->ordNo;
            $fs->tranAmt = $sxp->tranAmt;
            $fs->ouuid = $sxp->ouuid;
            $fs->posSeqNo = $sxp->posSeqNo;
            $fs->tranDt = $sxp->tranDt;
            $fs->searchNo = $sxp->searchNo;
            $fs->anhui_income_amt = intval($sxp->tranAmt * 100) - intval($sxp->feeAmt * 100);
            if(!$fs->wpos_id) {
                $fs->status = 2;
                $fs->exception_key = '01';
            }
            try {
                $fs->save();
            }catch(Exception $e){
                print_r($e);
                Log::error(json_encode($e->getMessage()));
                break;
            }
        }
    }
    /**
     * 调用 随行付 对账
     */
    public static function statement($wpos_type,$wfd_id){
        if($wpos_type == 2){
            $wfd = WposFinanceDetail::where('id', $wfd_id)->first();
        }else if($wpos_type == 1) {
            $wfd = WposInputRecords::find($wfd_id);
        }
        try{
            $thirdSerialno = $wfd->thirdSerialno;
            $orderno = $wfd->orderno;
            $fs = FinanceStatement::where(function($query) use($thirdSerialno)
            {
                $query->where('searchNo', $thirdSerialno)
                    ->orWhere('thirdSerialno', $thirdSerialno);
            }
            )->first();
            if($fs && $fs->tranSeqNo) {
                $fs1 = FinanceStatement::where('ouuid', $fs->tranSeqNo)->first();
            }
            if(!$fs){
                $fs = new FinanceStatement();
                $fs->created_at = Common::currentTimeString();
            }
            $fs->wpos_type = 2;
            $fs->wpos_id = $wfd->id;
            $fs->orderno = $orderno;
            if(isset($fs1)){
                $fs1->wpos_type = 2;
                $fs1->wpos_id = $wfd->id;
                $fs1->orderno = $orderno;
            }
            $wr = WposRecord::where('cashier_trade_no',$orderno)->first();
            if($wr) {
                $fs->shop_trans__no = '';
                $fs->shop_order_id = '';
                if(isset($fs1)){
                    $fs1->shop_trans__no = '';
                    $fs1->shop_order_id = '';
                }
                if ($wr->type == 1) {
                    $pr = AcountUserRecharge::find($wr->buss_id);
                    if ($pr) {
                        $fs->shop_trans__no = $pr->tran_no;
                        $fs->shop_order_id = $wr->buss_id;
                        if(isset($fs1)){
                            $fs1->shop_trans__no = $pr->tran_no;
                            $fs1->shop_order_id = $wr->buss_id;
                        }
                    }
                } elseif ($wr->type == 2) {
                    $order_p = OrderPayRecord::find($wr->buss_id);
                    if ($order_p) {
                        $fs->shop_trans__no = $order_p->tran_no;
                        $fs->shop_order_id = $order_p->order_id;
                        if(isset($fs1)){
                            $fs1->shop_trans__no = $order_p->tran_no;
                            $fs1->shop_order_id = $order_p->buss_id;
                        }
                    }
                } elseif ($wr->type == 3) {
                    $c_order_p = CarWashOrderPayRecord::find($wr->buss_id);
                    if ($c_order_p) {
                        $fs->shop_trans__no = $c_order_p->tran_no;
                        $fs->shop_order_id = $c_order_p->order_id;
                        if(isset($fs1)){
                            $fs1->shop_trans__no = $c_order_p->tran_no;
                            $fs1->shop_order_id = $c_order_p->buss_id;
                        }
                    }
                }
            }
            $device = MaintenancePosDevice::where('device_no',$wfd->deviceEn)->first();
            if($device){
                $fs->sn_no = $device->sn_no;
                if(isset($fs1)){
                    $fs1->sn_no = $device->sn_no;
                }
            }
            if(!$fs->status || !$fs->sxp_id){
                $fs->status = 2;
                $fs->exception_key = '02';
            }else if ($fs->tranAmt * 100 != $wfd->amount){
                $fs->status = 2;
                $fs->exception_key = '03';
            }else if($fs->tranSts != 'S' && !($wfd->status == 2 || $wfd->status == 3)){
                $fs->status = 2;
                $fs->exception_key = '04';
            }else if($fs->tranType == ''){
                $fs->status = 2;
                $fs->exception_key = '05';
            }else{
                $fs->status = 1;
                $fs->exception_key = '00';
            }
            if(isset($fs1)){
                if(!$fs1->status || !$fs1->sxp_id){
                    $fs1->status = 2;
                    $fs1->exception_key = '02';
                }else if ($fs1->tranAmt * 100 != $wfd->refundAmount){
                    $fs1->status = 2;
                    $fs1->exception_key = '03';
                }else if($fs1->tranSts != 'S' || $wfd->status != 2 || $wfd->status != 3){
                    $fs1->status = 2;
                    $fs1->exception_key = '04';
                }else if($fs1->tranType == ''){
                    $fs1->status = 2;
                    $fs1->exception_key = '05';
                }else{
                    $fs1->status = 1;
                    $fs1->exception_key = '00';
                }
                $fs1->device_en = $wfd->deviceEn;
//                $fs1->thirdSerialno = $thirdSerialno;
                $fs1->payModeId =is_string($wfd->payModeId)?intval($wfd->payModeId): $wfd->payModeId;;
                $fs1->cardIssuresNo = $wfd->cardIssuresNo;
                if($wpos_type == 1) {
                    $fs1->cardType = 0;
                    $fs1->cardOrg = '';
                }else if($wpos_type == 2){
                    $fs1->cardType = $wfd->cardType;
                    $fs1->cardOrg = $wfd->cardOrg;
                }
                $fs1->payDate = $wfd->payDate;
            }
            $fs->device_en = $wfd->deviceEn;
            $fs->thirdSerialno = $thirdSerialno;
            $fs->payModeId =is_string($wfd->payModeId)?intval($wfd->payModeId): $wfd->payModeId;;
            $fs->cardIssuresNo = $wfd->cardIssuresNo;
            if($wpos_type == 1) {
                $fs->cardType = 0;
                $fs->cardOrg = '';
            }else if($wpos_type == 2){
                $fs->cardType = $wfd->cardType;
                $fs->cardOrg = $wfd->cardOrg;
            }
            $fs->payDate = $wfd->payDate;
            try{
                $fs->save();
                if(isset($fs1)){
                    $fs1->save();
                }
            }catch(Exception $e){
                Log::info($e->getMessage());
            }
            if($wpos_type == 1) {
                $wfd->statement_status = 2;
                try{
                    $wfd->save();
                }catch(Exception $e){
                    Log::info($e->getMessage());
                }
            }
        }catch(Exception $e){
            Log::info($e->getMessage());
        }
    }
}